//============================================================================
//  PROBE.C -
//
//  Description:
//
//  Revision history:
//  --------------------------------------------------------------------------
//           200209    UN20 Frank
//                     Initial release
//           200211    PD43 Austin Liu
//                     Modify for HW-MAC version
//
//  Copyright (c) 2002 Winbond Electronics Corp. All rights reserved.
//============================================================================
#include "sysdef.h"

//============================================================================
int SendProbeRequest(PWB32_ADAPTER Adapter, struct SSID_Element *SSID)
{
	struct Management_Frame*    msgHeader;
	PUCHAR   msg;
	u16      iCurrentLocation = 0;
	u8 	 BasicRate[8], OpRate[MAX_OPT_RATE_SET], BasicRateCount, OpRateCount;
	u8	 i, j, BitIndex;
	u32	 BasicRateBitmap, OpRateBitmap, tmp;

	// Get the free data buffer
	if ((msg = MLMEGetMMPDUBuffer(Adapter)) == NULL) {
		// No available data buffer now. Stop send the MMPDU.
		Adapter->sMlmeFrame.wNumTxMMPDUDiscarded++;
		return -1;
	}
	msgHeader = (struct Management_Frame*) &msg[0];

	// first setup the frame_control header
	msgHeader->frame_control.mac_frame_info = MAC_SUBTYPE_MNGMNT_PROBE_REQUEST;
	msgHeader->frame_control.to_ds      = 0;  // = 0 for management frame
	msgHeader->frame_control.from_ds    = 0;  // = 0 for management frame
	msgHeader->frame_control.more_frag  = 0;
	msgHeader->frame_control.retry		= 0;
	msgHeader->frame_control.more_data  = 0;
	msgHeader->frame_control.order		= 0;
	msgHeader->frame_control.pwr_mgt    = 0;
	msgHeader->frame_control.WEP        = 0;
	msgHeader->duration                 = 0;  // Set by the MDS

	// now do the addresses
	memcpy( msgHeader->DA, "\xff\xff\xff\xff\xff\xff", MAC_ADDR_LENGTH );
	memcpy( msgHeader->SA, Adapter->sLocalPara.ThisMacAddress, MAC_ADDR_LENGTH );
	memcpy( msgHeader->BSSID, psSCAN->BSSID, MAC_ADDR_LENGTH );
	
	msgHeader->Sequence_Control         = 0;    //Will be filled by HW-MAC
	
	iCurrentLocation = sizeof(struct Management_Frame);

	// now copy in the SSID element
	memcpy( &msg[iCurrentLocation], SSID, SSID->Length+2 );
	iCurrentLocation += (SSID->Length+2);

	// now do the supported rates

	if (psLOCAL->bMacOperationMode == MODE_802_11_A) {
		BasicRateBitmap = LOCAL_11A_BASIC_RATE_BITMAP;
		OpRateBitmap = LOCAL_11A_OPERATION_RATE_BITMAP;
	}
	else if (psLOCAL->bMacOperationMode == MODE_802_11_B) {
		BasicRateBitmap = LOCAL_11B_BASIC_RATE_BITMAP;
		OpRateBitmap = LOCAL_11B_OPERATION_RATE_BITMAP;
	} else {
		BasicRateBitmap = LOCAL_11G_BASIC_RATE_BITMAP;
		OpRateBitmap = LOCAL_11G_OPERATION_RATE_BITMAP;
	}

	i = j = 0;
	for (BitIndex=0 ; BitIndex<32; BitIndex++) {
		tmp = 1<<BitIndex;
		if (BasicRateBitmap&tmp)
			BasicRate[i++] = 0x80 | bBitmapToRate(BitIndex);
		if (OpRateBitmap&tmp)
			OpRate[j++] = bBitmapToRate(BitIndex);
	}
	BasicRateCount = i;
	OpRateCount = j;

	DesiredRate2InfoElement(Adapter, msg, &iCurrentLocation,
				BasicRate, BasicRateCount,
				OpRate, OpRateCount);

	if( psLOCAL->IE_Append_size ) {
		memcpy(&msg[iCurrentLocation], psLOCAL->IE_Append_data, psLOCAL->IE_Append_size);
		iCurrentLocation += psLOCAL->IE_Append_size;
	}

	// now send this message out
	if (1 == MLMESendFrame(Adapter, (u8 *)&msg[0], iCurrentLocation,
			       FRAME_TYPE_802_11_MANAGEMENT))
		return 1;                           // Has sent to the Tx handler.
	else
	{
		//return MLME buffer
		MLMEfreeMMPDUBuffer(Adapter, msg);
		return -1;
	}

}

